package com.ruoyi.web.controller.community.integral;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.service.IIntSetService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.community.domain.integral.IntDetails;
import com.ruoyi.community.service.integral.IIntDetailsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 积分详情Controller
 * 
 * @author ruoyi
 * @date 2021-08-08
 */
@RestController
@RequestMapping("/system/details")
public class IntDetailsController extends BaseController
{
    @Autowired
    private IIntDetailsService intDetailsService;

    @Autowired
    private IIntSetService setService;

    /**
     * 查询积分详情列表
     */
    @PreAuthorize("@ss.hasPermi('system:details:list')")
    @GetMapping("/list")
    public TableDataInfo list(IntDetails intDetails)
    {
        startPage();
        List<IntDetails> list = intDetailsService.selectIntDetailsList(intDetails);
        return getDataTable(list);
    }

    /**
     * 每日首次登录
     */
    @GetMapping("/sign")
    public AjaxResult firstLogin() {
        IntDetails details = new IntDetails();
        details.setUserId(SecurityUtils.getUserID());
        details.setCommunityId(SecurityUtils.getCommunityId());
        Integer cnt = intDetailsService.isFirst(details);
        if (cnt == 0) {
        //    首次登录
        //    添加每日登录积分
            details.setOperationId(0);
            details.setContent("每日登录");
            details.setType(1);
            details.setIntegral(setService.selectPriceByName("0"));
            intDetailsService.insertIntDetails(details);
            return AjaxResult.success("每日签到成功");
        } else {
        //    非首次登录
            return AjaxResult.error("非首次登录");
        }
    }

    /**
     * 导出积分详情列表
     */
    @PreAuthorize("@ss.hasPermi('system:details:export')")
    @Log(title = "积分详情", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(IntDetails intDetails)
    {
        List<IntDetails> list = intDetailsService.selectIntDetailsList(intDetails);
        ExcelUtil<IntDetails> util = new ExcelUtil<IntDetails>(IntDetails.class);
        return util.exportExcel(list, "积分详情数据");
    }

    /**
     * 获取积分详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:details:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(intDetailsService.selectIntDetailsById(id));
    }

    /**
     * 新增积分详情
     */
    @PreAuthorize("@ss.hasPermi('system:details:add')")
    @Log(title = "积分详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IntDetails intDetails)
    {
        return toAjax(intDetailsService.insertIntDetails(intDetails));
    }

    /**
     * 修改积分详情
     */
    @PreAuthorize("@ss.hasPermi('system:details:edit')")
    @Log(title = "积分详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IntDetails intDetails)
    {
        return toAjax(intDetailsService.updateIntDetails(intDetails));
    }

    /**
     * 删除积分详情
     */
    @PreAuthorize("@ss.hasPermi('system:details:remove')")
    @Log(title = "积分详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(intDetailsService.deleteIntDetailsByIds(ids));
    }
}
